package com.browsesoft.resource;

import java.util.Hashtable;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.SequenceFind;
import com.browsesoft.dbtools.DBTools;

/**
 * 权限，这里是指各个资源之间的权限
 */
public class Right implements Entity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2225043469140084159L;

	/**
	 * 权限哈希
	 */
	Hashtable ht = new Hashtable();

	/**
	 * 权限类型
	 */

	RightType rightType = new RightType();

	/**
	 * 构造方法
	 * 
	 * @param res
	 *            资源
	 */
	public Right(Resource res) {
		try {
			this.ht.put("resourceid", res.getID());
			int id = SequenceFind.getID();
			this.ht.put("id", Integer.toString(id));
			this.insert();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 哈希构造
	 * 
	 * @param ht
	 */
	public Right(Hashtable ht) {
		this.ht.putAll(ht);
		this.rightType = new RightType();
		rightType.setType((String) ht.get("righttype"));
	}

	/**
	 * 取得ID
	 * 
	 * @return
	 */
	public String getID() {
		return (String) this.getAttributes().get("id");
	}

	/**
	 * 取得资源
	 * 
	 * @return
	 */
	public Resource getResource() {
		try {
			EntityManager entitymanager = (EntityManager) ManagerGroup
					.getManagerGroup().getManager("entity");
			return (Resource) entitymanager.getObject((String) this
					.getAttributes().get("resourceid"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 设置权限类型
	 * 
	 * @param t
	 *            权限类型
	 */
	public void setType(RightType t) {
		try {
			this.rightType = t;
			if (rightType != null && rightType.getType() != null
					&& !rightType.getType().equals("")) {
				this.ht.put("righttype", rightType.getType());
			} else {
				this.ht.put("righttype", "");
			}
			this.update();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 得到权限类型
	 * 
	 * @return 权限类型
	 */
	public RightType getType() {
		return this.rightType;
	}

	/**
	 * 增加
	 */
	public void insert() throws Exception {
		DBTools.insert("t_right", this.ht);
	}

	/**
	 * 修改
	 */
	public void update() throws Exception {
		DBTools.update("t_right", this.getID(), this.ht);
	}

	/**
	 * 删除
	 */
	public void delete() throws Exception {
	}

	/**
	 * 建立关系
	 */
	public void createRelation() {
	}

	/**
	 * 取得属性
	 */
	public Hashtable getAttributes() {
		return this.ht;
	}

	public String getTableName() {
		return "t_right";
	}

	public void sychronDelete() {
	}

	public void sychronUpdate(Hashtable attributes) {
	}
}